# coding: utf-8
from application import db
from flask.config import Config


class Order(db.Model):
    __tablename__ = 't_order'

    id = db.Column(db.BigInteger, primary_key=True, unique=True, comment='序号')
    order_sn = db.Column(db.String(50), unique=True, comment='凭证号')
    seller = db.Column(db.String(20), comment='卖家钱包')
    buyer = db.Column(db.String(20), comment='买家钱包')
    goodsNumber = db.Column(db.BigInteger, comment='商品编号')
    price = db.Column(db.Numeric(10, 2), comment='金额')
    note = db.Column(db.String(500), comment='备注信息')
    status = db.Column(db.Integer, comment='状态')
    express_status = db.Column(db.Integer, comment='快递状态')
    express_address_id = db.Column(db.Integer, comment='快递地址id')
    express_info = db.Column(db.Integer, comment='快递信息')
    orderIdCounter = db.Column(db.BigInteger, comment='链上订单id')
    blocknum = db.Column(db.Integer, comment='块高')
    blockHash = db.Column(db.String(200), unique=True, comment='块哈希')
    transactionhash = db.Column(db.String(200), unique=True, comment='交易哈希')
    created_time = db.Column(db.DateTime, nullable=False, comment='创建时间')
    updated_time = db.Column(db.DateTime, nullable=False, comment='更新时间')

    @property
    def pay_status(self):
        tmp_status = self.status
        if self.status == 1:
            tmp_status = self.express_status
            if self.express_status == 1 and self.comment_status == 0:
                tmp_status = -5
            if self.express_status == 1 and self.comment_status == 1:
                tmp_status = 1
        return tmp_status

    @property
    def status_desc(self):
        PAY_STATUS_DISPLAY_MAPPING = {
            "0": "订单关闭",
            "1": "支付成功",
            "-8": "待支付",
            "-7": "待发货",
            "-6": "待确认",
            "-5": "待评价"
        }
        # return app.config['PAY_STATUS_DISPLAY_MAPPING'][str( self.pay_status)]
        return PAY_STATUS_DISPLAY_MAPPING[str(self.pay_status)]

    @property
    def order_number(self):
        order_number = self.created_time.strftime("%Y%m%d%H%M%S")
        order_number = order_number + str(self.id).zfill(5)
        return order_number